import request from '@/utils/request'

export function download(url, params, filename) {
  return request({
    url: url,
    method: 'get',
    params: params,
    responseType: 'blob'
  }).then(res => {
    const content = res
    const blob = new Blob([content])
    const fileName = filename
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href)
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, fileName)
    }
  }).catch(r => {
    console.error(r)
  })
} 